package es.twentymobile.mule.modules.jpa.strategy;

import java.util.EnumSet;

import javax.persistence.EntityManager;

public class EntityManagerStrategyFactory {

	public enum ReceiveAction {
		DELETE, MERGE, PERSIST;
	}

	private static EnumSet<ReceiveAction> receiveActions = EnumSet.of(ReceiveAction.DELETE, ReceiveAction.MERGE, ReceiveAction.PERSIST);

	public synchronized static boolean isValidReceiveAction(String action) {
		boolean isMatch = false;
		for (ReceiveAction receiveAction : receiveActions) {
			if (receiveAction.name().equalsIgnoreCase(action)) {
				isMatch = true;
				break;
			}
		}
		return isMatch;
	}

	public synchronized static EntityManagerStrategy createEntityManagerCommand(EntityManager em, ReceiveAction action, Boolean flush) {
		EntityManagerStrategy emcmd = null;
		switch (action) {
		case DELETE:
			emcmd = new EntityManagerDeleteStrategy(em, flush);
			break;
		case MERGE:
			emcmd = new EntityManagerMergeStrategy(em, flush);
			break;
		case PERSIST:
			emcmd = new EntityManagerPersistStrategy(em, flush);
		}
		return emcmd;
	}
}
